public class MySingleList implements IList{

    static class ListNode{
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;

    public void createList(){
        ListNode node1=new ListNode(10);
        ListNode node2=new ListNode(20);
        ListNode node3=new ListNode(30);
        ListNode node4=new ListNode(40);
        ListNode node5=new ListNode(50);
        node1.next=node2;
        node2.next=node3;
        node3.next=node4;
        node4.next=node5;
        this.head=node1;
    }

    @Override
    public void display() {
        ListNode cur=head;
        while (cur!=null){
            System.out.print(cur.val+" ");
            cur=cur.next;
        }
        System.out.println("");
    }

    //头插法
    @Override
    public void addFirst(int data) {
        ListNode first=new ListNode(data);
        first.next=head;
        this.head=first;
    }
    //尾插法
    @Override
    public void addLast(int data) {
        ListNode last=new ListNode(data);
        if(head==null){
            head=last;
            return;
        }
        ListNode cur=this.head;
        while(cur.next!=null){
            cur=cur.next;
        }
        cur.next=last;
    }

    @Override
    public void addIndex(int index, int data) {
        int len=size();
        if(index<0||index>len){
            System.out.println("异常");
            return;
        }
        if(len==0){
            addFirst(data);
            return;
        }
        if(index==len){
            addLast(data);
            return;
        }
        ListNode cur=this.head;
        ListNode node=new ListNode(data);
        while(index-1!=0){
            cur=cur.next;
            index--;
        }
        node.next=cur.next;
        cur.next=node;
    }

    @Override
    public boolean contains(int key) {
        ListNode cur=this.head;
        while(cur!=null){
            if(cur.val==key){
                return true;
            }
            cur=cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {

        ListNode cur=this.head;
        if(cur.val==key){
            head=cur.next;
            return;
        }
        while (cur.next!=null){
            if(cur.next.val==key){
                cur.next=cur.next.next;
                return;
            }
            cur=cur.next;
        }
        System.out.println("不存在该节点");
    }

    @Override
    public void removeAllKey(int key) {
        if(head==null){
            return;
        }
        ListNode prev=this.head;
        ListNode cur=this.head.next;
        while(cur!=null){
            if(cur.val==key){
                prev.next=cur.next;
                cur=cur.next;
            }else {
                prev=cur;
                cur=cur.next;
            }
        }
        if(head.val==key){   //如果不放在最后面就会出问题
            head=head.next;
        }
    }

    @Override
    public int size() {
        int size=0;
        ListNode cur=head;
        while(cur!=null){
            size++;
            cur=cur.next;
        }
        return size;
    }

    @Override
    public void clear() {
        ListNode cur=head;
        while (cur!=null){
            ListNode node=cur.next;
            cur.next=null;
            cur=node.next;
        }
        head=null;//不加这个会导致头结点删不掉
    }


    public ListNode reverseList() {
        if(head==null){
            return head;
        }
        ListNode cur=head.next;
        head.next=null;
        while(cur!=null){
            ListNode pre=cur.next;
            cur.next=head;
            head=cur;
            cur=pre;
        }
        return head;
    }

    //求中间节点
    public ListNode middleNode() {
        if(head.next==null){
            return head;
        }
        ListNode fast=head;
        ListNode slow=head;
        while(fast!=null&&fast.next!=null){
            fast=fast.next.next;
            slow=slow.next;
        }
        return slow;
    }

    //求倒数节点
    public int kthToLast(int k) {
        if(head.next==null){
            return head.val;
        }
        ListNode fast=head;
        ListNode slow=head;
        for(int i=0;i<k-1;i++){
            fast=fast.next;
            if(fast==null){
                return -1;
            }
        }

        while(fast!=null&&fast.next!=null){
            slow=slow.next;
            fast=fast.next;
        }

        return slow.val;
    }
}
